#include <linux/module.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/freezer.h>

MODULE_AUTHOR("agui");
MODULE_DESCRIPTION("kernel thread example");
MODULE_LICENSE("GPL");

static struct task_struct * agui_task;

static int agui_thread(void * nothing)
{
        int count = 0;

        set_freezable();

        allow_signal(SIGKILL);

        do {
                set_current_state(TASK_INTERRUPTIBLE);
                schedule_timeout(10);

                count ++;
                if(count >= 100)
                {
                        printk("count is overflow 100, reset\n");
                        count = 0;
                }

        } while(!kthread_should_stop());

        printk("kernel thread exit\n");
        return 0;
}

static int __init thread_init(void)
{
        int error;

        agui_task = kthread_run(agui_thread, NULL, "ExampleThread");
        if(IS_ERR(agui_task))
        {
                error = PTR_ERR(agui_task);
                return error;
        }

        return 0;
}

static void __exit thread_exit(void)
{
        kthread_stop(agui_task);
}

module_init(thread_init);
module_exit(thread_exit);
